<?php

namespace Apps\AppApi\Method\User;

use Apps\AppApi\Method\BaseMethod;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\UserFollow;

/**
 * 取消关注用户/专家
 */
class UnFollow extends BaseMethod
{
    public function run()
    {
    	if(false == $this->verifyUserAuth()){
    		return false;
    	}

    	$user_id = intval($this->getDataItem('user_id', 0));
    	$to_user_id = intval($this->getDataItem('to_user_id', 0));
    	$type = intval($this->getDataItem('type', 0));

		if ($user_id == 0 || $to_user_id == 0 || $type == 0) {
			return $this->errorLog(ResultStatus::URL_PARAM_CANNOT_EMPTY, 'URL参数不全');
		}
		if (!in_array($type, ['1', '2'])) {
			return $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR, 'type类型错误！');
		}
		if ($user_id != $this->getUserAuth()->userId) {
			return $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR, '不能取消他人的关注！');
		}

		$UserFollow = new UserFollow();
		if ($UserFollow->unFollow($user_id, $to_user_id, $type) && $type == 2) { // 减少专家的粉丝数
			(new \Apps\Common\Models\UserBase())->setExpertFollowCount($to_user_id, 1, false);
		}

		$this->setResult(['success' => 1, 'message' => '取消关注成功']);
    }


}